 /**
This file is part of Studentport.

    Studentport is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Studentport is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Studentport.  If not, see <http://www.gnu.org/licenses/>.

**/

package com.groupalpha.studentport;

import com.httpclasses.perfectConnect;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

/**
 * 
 * Will present the user with an webview loaded with the htmlstring from perfectConnect.getEjCoursesEN().
 * if error occur an error toast message is presented. 
 * if any links from webview is selected a webbrowser will be launched within app and present the user with the selected webpage.
 * @author GroupAlpha
 *
 */
public class EjAvslKursEN extends Activity{
	
	/** Called when the activity is first cresssated. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ejavslkurs);
        final Button logoffButton = (Button) findViewById(R.id.button2);
        logoffButton.setBackgroundResource(R.drawable.logoff_button_en);
        final WebView browser = (WebView) findViewById(R.id.ejAvslView);

        browser.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        browser.setVerticalScrollBarEnabled(false);
        browser.getSettings().setBuiltInZoomControls(true);
        browser.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        browser.setWebViewClient(new WebViewClient(){
      		public boolean shouldOverrideUrlLoading(WebView view,String url){
      			if(url != null && (url.startsWith("https://")||url.startsWith("http://"))){
      				Intent i = new Intent("com.groupalpha.studentport.NEWSBROWSER").putExtra("adress", url);
      		//		ServiceList.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
      				startActivity(i);
      				
      				return true;
      			}else{
      				return false;
      			}
      			
      		}
      	});
        
        final ProgressDialog dialog = new ProgressDialog(EjAvslKursEN.this);
        final Toast toast = Toast.makeText(EjAvslKursEN.this, "Error", Toast.LENGTH_SHORT);
   	 
        dialog.setTitle("Loading Uncompleted courses");
   	 	dialog.setMessage("Wait...");
   	 dialog.setCancelable(true);
		dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				
			EjAvslKursEN.this.finish();
			}
		});
   	 	dialog.show();
     	 new Thread(){
 	 		public void run(){
 	 		try {
 	 		final String result = Uri.encode(perfectConnect.getInstance().getEjCoursesEN());
 	        	
 	        	EjAvslKursEN.this.runOnUiThread(new Runnable(){public void run(){
 	        		browser.loadData(result , "text/html", "UTF-8");
 	        		dialog.dismiss();
 	        		
 	        	}});
			} catch (Exception e) {
				// TODO Auto-generated catch block
				toast.show();
				dialog.dismiss();
			}
	 			
	 		}
	 	}.start();
   	 		
	 	logoffButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(EjAvslKursEN.this, StudentportActivity.class); 
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes other Activities from stack 
				startActivity(intent);
            }
            });
    }
    }

